from django.shortcuts import render
from django.http import HttpResponse,JsonResponse
from .. models import Order,Goods,OrderInfo


from django.core.paginator import Paginator

# Create your views here.
# 订单首页
def orderindex(request):
	
	# ob = Order.objects.all()
	# context = {'order':ob}

	v = request.GET.get('keywords','')

	arr = {
		1:['未付款'],
		2:['已付款,待发货','已付款'],
		3:['已发货,待收货','已发货'],
		4:['已完成'],
		5:['已取消']
	}
	status=0
	for k,x in arr.items():
		if v in x:
			status = k
	# Q对象
	from django.db.models import Q

	ob = Order.objects.filter(Q(id__contains=v)|Q(addname__contains=v)|Q(address__contains=v)|Q(totalprice__contains=v)|Q(status__contains=status))

	 # 实例化分页类
	paginator = Paginator(ob,2)

	# 获取当前页码
	p = int(request.GET.get('p',1))

	# 获取分页数据对象
	orderlist= paginator.page(p)

	# 分配数据

	context = {'order':orderlist,'p':p}


	return render(request,'myadmin/order/index.html',context)

# 订单修改
def orderedit(request,oid):
	ob = Order.objects.get(id=oid)
	
	return render(request,'myadmin/order/edit.html',{'oinfo':ob})

# 修改处理
def orderupdate(request):
	
	try:
		ob = Order.objects.get(id=request.POST['oid'])

		ob.addname = request.POST['addname']
		ob.address = request.POST['address']
		ob.addphone = request.POST['addphone']
		ob.status = request.POST['status']
		ob.save()

	# return HttpResponse('1')
		
		return HttpResponse('<script>alert("修改成功");location.href="/admin/orderindex"</script>')
	except:
		return HttpResponse('<script>alert("修改失败");location.href="/admin/orderedit/'+ob.id+'"</script>')

